﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace tamlytre.Admin
{
    public partial class Default : System.Web.UI.Page
    {
        public static int count = 0;
        DBTuVanDataContext db = new DBTuVanDataContext();
        public void loadNews()
        {
            try
            {
                List<TB_New> loadnews = db.USP_LoadAllNewsCate().ToList();
                ListViewUsers.DataSource = loadnews;
                ListViewUsers.DataBind();
                count = loadnews.Count();
            }
            catch
            {
                Response.Redirect("Default.aspx");
            }
        }
        public bool DeleteNews(int id)
        {
            try
            {
                using (DBTuVanDataContext db = new DBTuVanDataContext())
                {
                    TB_New tk = db.TB_News.Single(t => t.idNews == id);
                    tk.DaXoa = true;
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        public void DoDelete()
        {
            string action = Request.QueryString["del"];
            if (action == null)
                return;
            int idnews = 0;
            if (Int32.TryParse(action, out idnews))
            {
                DeleteNews(idnews);
                Response.Redirect("../Admin/Default.aspx");
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                loadNews();
            DoDelete();
        }
        
        protected void DataPager_PreRender(object sender, EventArgs e)
        {
            loadNews();
        }
    }
}